package com.example.administrator.demo_one;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.demo_one.Util.InitialPeopleList;
import com.example.administrator.demo_one.VO.People;
import com.example.administrator.demo_one.VO.PeopleAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private final static int SecondActivityRequestCode = 1;
    private Button button;
    private Button button2;
    private TextView textView;
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);//活动不显示标题 需要继承自Activity
        setContentView(R.layout.activity_main);
//        if (getSupportActionBar() != null){//实现了活动不显示标题
//            getSupportActionBar().hide();
//        }
        Log.d("MainActivity","----------------test------------");
        button = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        textView = (TextView) findViewById(R.id.text1);
        listView = (ListView) findViewById(R.id.listview1);

        //listview初始化
        List<People>peoples = new ArrayList<People>();
        InitialPeopleList.initialPeopleList(peoples);
        PeopleAdapter peopleAdaper = new PeopleAdapter(MainActivity.this,R.layout.simple_item,peoples);
        listView.setAdapter(peopleAdaper);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //使用隐式intent调用浏览器和电话本
                Intent intent = new Intent(Intent.ACTION_VIEW);//Intent.ACTION_DIAL
                intent.setData(Uri.parse("http://www.baidu.com"));//Uri.parse("tel:10086")
                startActivity(intent);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivityForResult(intent,SecondActivityRequestCode);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case SecondActivityRequestCode:
                if (resultCode==RESULT_OK){
                    Bundle bundle = data.getBundleExtra("data");
                    textView.setText(bundle.getString("data"));
                }
        }
    }

    /*
    * 加载menu布局文件，在这个Activity中显示出来
    * */
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }
    /*
    * 定义菜单的点击事件
    * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.add_item:
                Toast.makeText(this,"你点击了添加",Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete_item:
                Toast.makeText(this,"你点击了删除",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}
